
/**
 * 
 * @param jsonData_ 流程图json序列号信息，对于 WF_DE_DEPLOY.JSONDATA_ 字段
 * @param top
 * @param left
 * @param currentMissionName 历史执行节点
 * @returns
 */
function WorkFlow(jsonData_, top, currentMissionName) {
	
	// 校验参数
	if(!arguments[1]) {
		top = 0;
	}
  	if(!arguments[2]) {
  		currentMissionName = "";
  	}
	
  	this.initWorkFlowDiv(top);
  	
  	// 解析为Json对象
  	var jsons = jsonData_.split("$$");
  	var missionData = $.parseJSON(jsons[0]);
  	var lineData = $.parseJSON(jsons[1]);
  	this.currentMissionName = currentMissionName;
	
	this.initMission(missionData);
	
	this.initLines(lineData);
	
	window.wf = this;
}

WorkFlow.prototype = {
		
		// 当前执行的MissionName，可以为空
		currentMissionName : '',
		
		// 历史执行路径播放到Index
		historyIndex : -1,
		// 历史执行路径播放间隔时间
		interval : 1.5,
		// 线闪烁间隔时间
		lineInterval : 300,
		// 线颜色变化
		lineColorChangeFlag : true,
		
		/**
		 * 初始化DOM节点：workflow-container
		 */ 
		initWorkFlowDiv : function(top) {
			
			var workflowDiv = $("#workflow-container");
			if(workflowDiv) {
				workflowDiv.remove();
			}
			
			workflowDiv = $(`<div id="workflow-container" style="top: ${top}px;"></div>`);
			this.workflowDiv = workflowDiv;
			$(document.body).append(workflowDiv);
			
		},
		
		initMission : function(jsonData) {
			
			this.missions = new Array();
			var max, maxY = 0, minX = 2000;
			
			for ( var i = 0; i < jsonData.length; i++) {
				
				var data = jsonData[i];
				var mission = new UserTask(data, this.workflowDiv);
				if(mission.missionName == this.currentMissionName) {
					mission.isCurrent = true;
				}
				this.missions.push(mission);
				
				if(mission.y > maxY) {
					maxY = mission.y + mission.h;
				}
				if(mission.x < minX) {
					minX = mission.x;
				}
				
				if(max) {
					if(mission.x > max.x) {
						max = mission;
					}
				} else {
					max = mission;
				}
			}
			
			// 设置workflowDiv的width，是为了让其transform: translate(-50%, 0);生效，让流程图在X轴居中
			this.workflowDiv.css('width', ((max.x + max.w - minX) + (minX * 2)) + 'px');
			this.workflowDiv.css('height', (maxY + 80) + 'px');
		},
		
		initLines : function(jsonData) {
	
			this.lines = new Array();
			
			for ( var i = 0; i < jsonData.length; i++) {
				var data = jsonData[i];
				var line = new Line(data, this.workflowDiv);
				this.lines.push(line);
			}
			
		},
		
		show : function() {
			for(var i=0; i<this.missions.length; i++)  {
				this.missions[i].show();
			}
			for(var i=0; i<this.lines.length; i++)  {
				this.lines[i].show();
			}
		},
		
		toggle : function() {
			if(this.workflowDiv.css("display") == "none"){
				this.workflowDiv.show();
			} else {
				this.workflowDiv.hide();
			}
		},
		
		/**
		 * 播放历史执行流程；如果参数historyMission和this.historyMission为空。则返回
		 * 否则，historyMission 优先级 高于 this.historyMission
		 */
		historyProcess : function(historyMission) {
			
			if(this.timer) {
				return ;
			}
			
			if(!this.historyMission && !historyMission) {
				return ;
			}
			
			if(historyMission) {
				this.historyMission = historyMission;
			}
			
			/**
			 * 播放执行节点
			 */
			this.timer = setInterval(function() {
				var $this = window.wf;
				
				// 播放到最后执行任务时，则重头播放
				$this.historyIndex ++;
				if($this.historyIndex == $this.historyMission.length) {
					$this.historyIndex = 0;
				}
				
				// 播放HistoryMission[historyIndex]节点
				for(var i=0; i<$this.missions.length; i++) {
					$this.missions[i].reset();
					if($this.historyMission[$this.historyIndex].name == $this.missions[i].missionName) {
						$this.missions[i].historyProcess();
					}
				}
			}, this.interval * 1000);
			
			/**
			 * 播放流程扭转的指向
			 */
			this.lineTimer = setInterval(function() {
				var $this = window.wf;
				
				if($this.historyIndex == -1) {
					return ;
				}
				
				var transferLines = $this.historyMission[$this.historyIndex].transferName;
				// 该节点可能为currentMissionName；则transferName为空
				if(transferLines) {
					for(var i=0; i<$this.lines.length; i++) {
						
						$this.lines[i].reset();
						var list = transferLines.split(",");
						
						// 修改线的颜色
						if(list.contains($this.lines[i].name)) {
							if($this.lineColorChangeFlag) {
								$this.lines[i].setColor("#ff6666");
							} else {
								$this.lines[i].setColor(line_initColor);
							}
						}
					}
					
					$this.lineColorChangeFlag = !$this.lineColorChangeFlag;
				} else {
					for(var i=0; i<$this.lines.length; i++) {
						$this.lines[i].reset();
					}
				}
				
			}, this.lineInterval);
			
		},
		
		// 重新播放历史执行流程
		startHistoryProcess : function() {
			this.historyProcess();
		},
		
		// 停止播放历史执行流程
		stopHistoryProcess : function() {
			window.clearInterval(this.timer);
			window.clearInterval(this.lineTimer);
			this.timer = null;
			this.lineTimer = null;
		},
		
		// 上一个执行节点
		prevProcess : function() {
			this.stopHistoryProcess();
			var $this = this;
			$this.historyIndex --;
			
			if($this.historyIndex < 0) {
				$this.historyIndex = $this.historyMission.length - 1;
			}
			
			for(var i=0; i<$this.missions.length; i++) {
				$this.missions[i].reset();
				if($this.historyMission[$this.historyIndex].name == $this.missions[i].missionName) {
					$this.missions[i].historyProcess();
				}
			}
			
		},
		
		// 下一个执行节点
		nexProcess : function() {
			this.stopHistoryProcess();
			var $this = this;
			$this.historyIndex ++;
			
			if($this.historyIndex == $this.historyMission.length) {
				$this.historyIndex = 0;
			}
			
			for(var i=0; i<$this.missions.length; i++) {
				$this.missions[i].reset();
				if($this.historyMission[$this.historyIndex].name == $this.missions[i].missionName) {
					$this.missions[i].historyProcess();
				}
			}
			
		}

}



